package com.tech15.core.action;

import java.util.Map;

import javacommon.base.BaseStruts2Action;
import javacommon.util.DateTools;
import javacommon.util.LoginInfo;
import javacommon.util.Notes;
import javacommon.util.ResInfo;

import org.hibernate.exception.GenericJDBCException;

import com.tech15.core.model.SysUser;
import com.tech15.core.service.SysUserManager;
import com.tech15.pub.model.Attendance;
import com.tech15.pub.service.AttendanceManager;

public class LoginAction extends BaseStruts2Action{

	private static final long serialVersionUID = 1L;
	
	protected static final String HOME_JSP = "index.jsp";
	protected static final String LOGOUT_JSP = "logout.html";
	protected static final String LOGIN_JSP = "login.jsp";
	protected static final String TIMEOUT_JSP = "timeout.jsp";
	
	protected static final String INDEX_ACTION = "!/index.do";
	
	private SysUserManager sysUserManager;
	private AttendanceManager attendanceManager;
	
	public void setAttendanceManager(AttendanceManager manager) {
		this.attendanceManager = manager;
	}

	public String login(){
		Map<String, Object> resInfo = null;
		if(isNullOrEmptyString(username) || isNullOrEmptyString(password)){
			resInfo = ResInfo.getResInfo("",false, false, 1, "你还未登入！","");
			this.addActionError("你还未登入！");
			return LOGIN_JSP;
		}else{
			try{
				LoginInfo loginInfo = sysUserManager.login(username,password);
				if(loginInfo != null){
					if(loginInfo.getState() == 0){
						resInfo = ResInfo.getResInfo("",false, false, 1, "此用户未激活,请联系系统管理员!","");
						this.addActionError("此用户未激活,请联系系统管理员!");
						return LOGIN_JSP;
					}else{
						getRequest().getSession().removeAttribute("rand");
						getRequest().getSession().setAttribute("loginInfo", loginInfo);
						resInfo = ResInfo.getResInfo("",false, false, 2, "操作成功","");
						this.addActionError("操作成功");
						
						/*SysUser u = sysUserManager.getById(loginInfo.getUserid());
						if(u.getLogincount() == null || u.getLogincount() == 0){
							
						}*/
						
						/*Attendance att = new Attendance();
						att.setUserid(loginInfo.getUserid());
						att.setRegtime(DateTools.getCurrentTime());
						att.setCurdate(DateTools.getDisplayDate());
						att.setIsauto(0);
						attendanceManager.saveAtt(att);*/
					}
				}else{
					resInfo = ResInfo.getResInfo("",false, false, 1, "登录失败，用户名或密码错误！","");
					this.addActionError("登录失败，用户名或密码错误！");
					return LOGIN_JSP;
				}
			}catch(GenericJDBCException e){
				//resInfo = ResInfo.getResInfo("",false, false, 1, "登录失败，用户名或密码错误！","");
				this.addActionError("登录失败，数据库连接错误！");
				return LOGIN_JSP;
			}
			return INDEX_ACTION;
		}
		//outXmlJson(resInfo);
	}

	public String index(){
		LoginInfo loginInfo = (LoginInfo) getRequest().getSession().getAttribute("loginInfo");
		if(loginInfo == null){
			return LOGOUT_JSP;
		}else{
			
			/*Map map = userManager.findAllFunByUserId(loginInfo.getUserid());
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.setExcludes(new String[] { "function", "functions" });
			JSON json = JSONSerializer.toJSON(map, jsonConfig);  
			System.out.println(json.toString());
			
			getRequest().setAttribute("funList", json.toString());*/
		}
		return HOME_JSP;
	}
	
	public String logout(){
		getRequest().getSession().removeAttribute(Notes.LOGIN_INFO);
		//String host = getRequest().getHeader("Host");
		return LOGIN_JSP;
	}
	
	public String forward(){
		LoginInfo loginInfo = (LoginInfo) getRequest().getSession().getAttribute("loginInfo");
		if(loginInfo == null){
//			String host = getRequest().getHeader("Host");
//			setDomain(host);
			return LOGIN_JSP;
		}else{
			return INDEX_ACTION;
		}
	}
	
	public String timeout(){
		LoginInfo loginInfo = (LoginInfo) getRequest().getSession().getAttribute("loginInfo");
		if(loginInfo == null){
			return TIMEOUT_JSP;
		}else{
			return INDEX_ACTION;
		}
	}
	
	
	private String username;
	private String password;
	private String checkCode;
	private String redirectUrl;
	private String domain;

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	public String getCheckCode() {
		return checkCode;
	}

	public void setCheckCode(String checkCode) {
		this.checkCode = checkCode;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRedirectUrl() {
		return redirectUrl;
	}

	public void setRedirectUrl(String redirectUrl) {
		this.redirectUrl = redirectUrl;
	}

	public void setSysUserManager(SysUserManager sysUserManager) {
		this.sysUserManager = sysUserManager;
	}


}
